<template>
    <div class="wallet-container" style="margin-bottom: 50px; padding-bottom: 10px;">
        <van-nav-bar title="我的钱包" left-text="" fixed>
        </van-nav-bar>

        <div class="content">
            <!-- 加载状态 -->
            <div v-if="isLoading" class="loading-container">
                <van-loading type="spinner" color="#4f46e5" />
                <p class="loading-text">正在加载钱包信息...</p>
            </div>

            <!-- 钱包信息，仅在非加载状态显示 -->
            <template v-else>
                <!-- 钱包卡片 -->
                <div class="wallet-card">
                    <div class="flex justify-between items-center mb-4">
                        <h2 class="text-lg font-semibold">账户余额</h2>
                    </div>
                    <div style="font-size: 1.5em; margin-bottom: 16px;">¥{{ walletInfo.balance.toFixed(2) }}</div>
                    <div class="flex" style="display: flex; justify-content: center;">
                        <van-button class="wallet-action-btn" style="margin-right: 10px;" @click="goToWithdraw">
                            <van-icon name="cash-back-record" class="mr-1" /> 提现
                        </van-button>
                        <van-button class="wallet-action-btn" @click="goToRecharge">
                            <van-icon name="plus" class="mr-1" /> 充值
                        </van-button>
                    </div>
                </div>

                <!-- 收支统计 -->
                <div class="stats-card">
                    <div class="stats-header">
                        <h3 class="section-title">收支统计</h3>
                    </div>
                    <div class="stats-content">
                        <div class="stats-item income">
                            <div class="stats-label">收入</div>
                            <div class="stats-value">¥{{ totalIncome.toFixed(2) }}</div>
                        </div>
                        <div class="stats-item expense">
                            <div class="stats-label">支出</div>
                            <div class="stats-value">¥{{ totalWithdraw.toFixed(2) }}</div>
                        </div>
                    </div>
                </div>

                <!-- 交易记录 -->
                <div class="transactions-card">
                    <div class="tab-container">
                        <div v-for="(tab, index) in tabs" :key="index"
                            :class="['tab-item', { active: activeTab === index }]" @click="activeTab = index">
                            {{ tab }}
                        </div>
                    </div>

                    <div v-if="transactions.length === 0" class="empty-state">
                        <van-empty description="暂无交易记录" />
                    </div>

                    <div v-else class="transaction-list">
                        <div v-for="(transaction, index) in filteredTransactions" :key="index" class="transaction-item">
                            <div class="transaction-row">
                                <div
                                    :class="['transaction-icon', transaction.transactionType === 0 ? 'income-icon' : 'expense-icon']">
                                    <van-icon :name="transaction.transactionType === 0 ? 'balance-o' : 'balance-pay'" />
                                </div>
                                <div class="transaction-title">{{ transaction.description }}</div>
                                <div class="transaction-right">
                                    <span class="transaction-time">{{ transaction.createTime }}</span>
                                    <span
                                        :class="['transaction-amount', transaction.transactionType === 0 ? 'income-amount' : 'expense-amount']">
                                        {{ transaction.transactionType === 0 ? '+' : '-' }}¥{{
                                            (transaction.transactionAmount || 0).toFixed(2) }}
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </template>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { showToast, showDialog } from 'vant'
import network from '../network'

const isLoading = ref(true)
const activeTab = ref(0)

// 钱包信息
const walletInfo = reactive({
    walletId: 0,
    userId: 0,
    balance: 0,
    frozenBalance: 0,
    totalIncome: 0,
    totalWithdraw: 0,
    walletStatus: 0,
    payPassword: '',
    salt: '',
    createTime: '',
    updateTime: ''
})

// 时间范围选项
const timeOptions = [
    { text: '本周', value: 'week' },
    { text: '本月', value: 'month' },
    { text: '今年', value: 'year' }
]

// 标签页
const tabs = ['全部', '收入', '支出']

// 交易记录
const transactions = ref<any[]>([])

const totalWithdraw = computed(() => {
    return transactions.value
        .filter(item => item.transactionType === 1)
        .reduce((sum, item) => sum + item.transactionAmount, 0)
})

const totalIncome = computed(() => {
    return transactions.value
        .filter(item => item.transactionType === 0)
        .reduce((sum, item) => sum + item.transactionAmount, 0)
})

// 获取钱包信息
const fetchWalletInfo = async () => {
    try {
        isLoading.value = true
        const res: any = await network.getWalletInfo()
        if (res && res.code === 100) {
            const data = res.data
            Object.assign(walletInfo, {
                walletId: data.walletId,
                userId: data.userId,
                balance: data.balance,
                frozenBalance: data.frozenBalance,
                totalIncome: data.totalIncome,
                totalWithdraw: data.totalWithdraw,
                walletStatus: data.walletStatus,
                payPassword: data.payPassword,
                salt: data.salt,
                createTime: data.createTime,
                updateTime: data.updateTime
            })
        } else {
            showToast('获取钱包信息失败')
        }
    } catch (error) {
        console.error('获取钱包信息失败:', error)
        showToast('获取钱包信息失败，请稍后重试')
    } finally {
        isLoading.value = false
    }
}

// 获取交易记录
const fetchTransactions = async () => {
    try {
        const res: any = await network.getTransactions()
        if (res && res.code === 100) {
            transactions.value = res.data.map((item: any) => ({
                ...item,
                // 确保交易类型是数字
                transactionType: Number(item.transactionType),
                // 确保交易金额是数字
                transactionAmount: Number(item.transactionAmount),
                // 确保余额是数字
                balance: Number(item.balance)
            }))
        } else {
            transactions.value = []
        }
    } catch (error) {
        console.error('获取交易记录失败:', error)
        transactions.value = []
    }
}

// 根据标签筛选交易记录
const filteredTransactions = computed(() => {
    if (activeTab.value === 0) {
        return transactions.value
    } else if (activeTab.value === 1) {
        return transactions.value.filter(item => item.transactionType === 0)
    } else {
        return transactions.value.filter(item => item.transactionType === 1)
    }
})

// 跳转到提现页面
const goToWithdraw = () => {
    showDialog({
        title: '提现',
        message: '开发中...',
    })
}

// 跳转到充值页面
const goToRecharge = () => {
    showDialog({
        title: '提现',
        message: '开发中...',
    })
}

// 页面加载时获取数据
onMounted(async () => {
    await fetchWalletInfo()
    await fetchTransactions()
})
</script>

<style scoped>
.wallet-container {
    min-height: 100vh;
    background-color: #f5f7fa;
    padding-top: 46px;
}

.content {
    max-width: 480px;
    margin: 0 auto;
    padding: 16px;
}

.wallet-card {
    background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
    color: white;
    border-radius: 16px;
    box-shadow: 0 10px 25px -5px rgba(79, 70, 229, 0.3);
    padding: 24px;
    margin-bottom: 16px;
    transition: all 0.3s ease;
}

.wallet-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 15px 30px -5px rgba(79, 70, 229, 0.4);
}

.wallet-buttons {
    gap: 32px;
    /* 设置更大的间距 */
    justify-content: center;
    /* 按钮居中对齐 */
}

.wallet-action-btn {
    background: rgba(255, 255, 255, 0.2);
    color: white;
    border: none;
    border-radius: 12px;
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.3s ease;
}

.wallet-action-btn:hover {
    background: rgba(255, 255, 255, 0.3);
}

.stats-card,
.transactions-card,
.bank-card {
    background-color: #ffffff;
    border-radius: 16px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
    padding: 20px;
    margin-bottom: 16px;
}

.stats-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.section-title {
    font-size: 18px;
    font-weight: 600;
    color: #1e293b;
}

.stats-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.stats-item {
    padding: 16px;
    border-radius: 12px;
}

.stats-item.income {
    background-color: rgba(16, 185, 129, 0.1);
}

.stats-item.expense {
    background-color: rgba(239, 68, 68, 0.1);
}

.stats-label {
    font-size: 14px;
    color: #64748b;
    margin-bottom: 8px;
}

.stats-value {
    font-size: 20px;
    font-weight: 600;
}

.income .stats-value {
    color: #10b981;
}

.expense .stats-value {
    color: #ef4444;
}

.tab-container {
    display: flex;
    border-bottom: 1px solid #e5e7eb;
    margin-bottom: 16px;
}

.tab-item {
    flex: 1;
    text-align: center;
    padding: 12px 0;
    font-size: 16px;
    font-weight: 500;
    color: #64748b;
    cursor: pointer;
    transition: all 0.3s ease;
}

.tab-item.active {
    color: #4f46e5;
    border-bottom: 2px solid #4f46e5;
}

.transaction-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.transaction-item {
    padding: 0;
    background: none;
    box-shadow: none;
    border-radius: 0;
}

.transaction-row {
    display: flex;
    align-items: center;
    padding: 14px;
    border-bottom: 1px solid #f1f5f9;
    background: linear-gradient(120deg, #f8fafc 60%, #f3f4f6 100%);
    border-radius: 12px;
    box-shadow: 0 2px 12px 0 rgba(79, 70, 229, 0.07);
    transition: box-shadow 0.25s, background 0.18s;
}

.transaction-row:last-child {
    border-bottom: none;
}

.transaction-icon {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 18px;
    font-size: 22px;
    flex-shrink: 0;
}

.income-icon {
    background: linear-gradient(135deg, #d1fae5 0%, #6ee7b7 100%);
    color: #10b981;
}

.expense-icon {
    background: linear-gradient(135deg, #fee2e2 0%, #fca5a5 100%);
    color: #ef4444;
}

.transaction-title {
    flex: 1;
    font-size: 16px;
    font-weight: 600;
    color: #22223b;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.transaction-right {
    display: flex;
    align-items: center;
    gap: 18px;
    min-width: 180px;
    justify-content: flex-end;
}

.transaction-time {
    font-size: 13px;
    color: #94a3b8;
    margin-right: 0;
    white-space: nowrap;
}

.transaction-amount {
    font-size: 18px;
    font-weight: 700;
    min-width: 80px;
    text-align: right;
}

.income-amount {
    color: #10b981;
}

.expense-amount {
    color: #ef4444;
}

.transaction-item:hover {
    box-shadow: 0 6px 24px 0 rgba(79, 70, 229, 0.13), 0 2px 8px 0 rgba(60, 60, 60, 0.06);
    transform: translateY(-2px) scale(1.012);
    background: linear-gradient(120deg, #f1f5f9 60%, #e0e7ef 100%);
}

.transaction-icon {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 16px;
    box-shadow: 0 2px 8px rgba(16, 185, 129, 0.07);
    font-size: 22px;
    transition: background 0.3s, transform 0.2s;
}

.income-icon {
    background: linear-gradient(135deg, #d1fae5 0%, #6ee7b7 100%);
    color: #10b981;
}

.expense-icon {
    background: linear-gradient(135deg, #fee2e2 0%, #fca5a5 100%);
    color: #ef4444;
}

.transaction-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.transaction-title {
    font-size: 16px;
    font-weight: 600;
    color: #22223b;
    margin-bottom: 4px;
    letter-spacing: 0.02em;
    transition: color 0.2s;
}

.transaction-item:hover .transaction-title {
    color: #4f46e5;
}

.transaction-time {
    font-size: 12px;
    color: #94a3b8;
    letter-spacing: 0.01em;
}

.transaction-amount {
    font-size: 18px;
    font-weight: 700;
    min-width: 90px;
    text-align: right;
    transition: color 0.2s, transform 0.2s;
    letter-spacing: 0.02em;
}

.transaction-item:hover .transaction-amount {
    transform: scale(1.08);
}

.income-amount {
    color: #10b981;
    text-shadow: 0 1px 0 #e6f9f0;
}

.expense-amount {
    color: #ef4444;
    text-shadow: 0 1px 0 #fde8e8;
}

.transaction-item::after {
    content: "";
    position: absolute;
    left: 0;
    top: 18px;
    bottom: 18px;
    width: 4px;
    border-radius: 4px;
    /* background: linear-gradient(180deg, #4f46e5 0%, #7c3aed 100%); */
    opacity: 0.08;
    transition: opacity 0.2s;
}

.transaction-item:hover::after {
    opacity: 0.18;
}

.view-all {
    text-align: center;
    padding: 18px 0 2px 0;
}

.view-all-btn {
    border: none;
    background: linear-gradient(90deg, #ede9fe 0%, #f3f4f6 100%);
    color: #4f46e5;
    font-size: 15px;
    font-weight: 600;
    border-radius: 10px;
    padding: 8px 26px;
    box-shadow: 0 2px 8px rgba(79, 70, 229, 0.07);
    transition: background 0.2s, color 0.2s, transform 0.18s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.view-all-btn:hover {
    background: linear-gradient(90deg, #c7d2fe 0%, #e0e7ef 100%);
    color: #3730a3;
    transform: translateY(-2px) scale(1.04);
}

.view-all-btn .van-icon {
    margin-left: 6px;
    transition: transform 0.18s;
}

.view-all-btn:hover .van-icon {
    transform: translateX(3px);
}

.bank-card-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    margin-bottom: 16px;
    transition: all 0.3s ease;
}

.bank-card-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.bank-icon {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    background-color: rgba(79, 70, 229, 0.1);
    color: #4f46e5;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 12px;
}

.bank-name {
    font-size: 15px;
    font-weight: 500;
    color: #1e293b;
    margin-bottom: 4px;
}

.card-number {
    font-size: 12px;
    color: #64748b;
}

.card-arrow {
    color: #94a3b8;
}

.add-card-btn {
    width: 100%;
    height: 44px;
    border: 1px solid #4f46e5;
    color: #4f46e5;
    background-color: transparent;
    border-radius: 12px;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.3s ease;
}

.add-card-btn:hover {
    background-color: rgba(79, 70, 229, 0.05);
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 24px 0;
}

.loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 300px;
    background-color: #ffffff;
    border-radius: 16px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
    padding: 20px;
    margin-bottom: 16px;
}

.loading-text {
    margin-top: 16px;
    font-size: 14px;
    color: #64748b;
}

:deep(.van-dropdown-menu) {
    height: 28px;
    background-color: transparent;
}

:deep(.van-dropdown-menu__bar) {
    box-shadow: none;
}

:deep(.van-dropdown-menu__title) {
    font-size: 14px;
    color: #64748b;
}

:deep(.van-empty__image) {
    width: 80px;
    height: 80px;
}

:deep(.van-empty__description) {
    color: #94a3b8;
    margin-bottom: 16px;
}
</style>